home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / idlenotifier.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  3.9 KB  |  97 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. """This module provides IdleNotifier objects which run in the background and 
  19.    notify delegate objects about the system idle state. The low-level platform
  20.    specific code which checks the actual idle state must be provided by the
  21.    frontend.
  22.    
  23.    An IdleNotifier object is controlled by two parameters: the idle threshold
  24.    and the periodicity. The periodicity value specifies the frequency at which
  25.    the system's idle state will be checked. When the system has been idling for 
  26.    more than idleThreshold seconds, the delegate's systemHasBeenIdlingSince 
  27.    method is called. When the system is active and is known to have been 
  28.    previously idling, the delegate's systemIsActiveAgain method is called.
  29. """
  30.  
  31. import time
  32.  
  33. import eventloop
  34. import idletime
  35. import logging
  36.  
  37. DEFAULT_PERIODICITY = 5             # Check every X seconds
  38. DEFAULT_IDLE_THRESHOLD = 60 * 5     # Notify after X seconds of idling
  39.  
  40.  
  41. class IdleNotifier:
  42.     def __init__(self, delegate, idleThreshold=DEFAULT_IDLE_THRESHOLD, periodicity=DEFAULT_PERIODICITY):
  43.         """Initialize the IdleNotifier object, the passed delegate will be
  44.            called when the system idle state changes.
  45.            - idleThreshold specifies the idle time (in seconds) after which the
  46.            delegate is called.
  47.            - periodicity specifies that that the idle state should be checked
  48.            every X seconds.
  49.         """
  50.         self.idling = False
  51.         self.wasIdling = False
  52.         self.delegate = delegate
  53.         self.idleThreshold = idleThreshold
  54.         self.periodicity = periodicity
  55.         self.lastTimeout = None
  56.  
  57.     def start(self):
  58.         logging.info ("idle notifier running")
  59.         self.lastTimeout = eventloop.addTimeout(self.periodicity,self.run,"Idle notifier")
  60.  
  61.     def run(self):
  62.         try:
  63.             seconds = int(idletime.get())
  64.         except:
  65.             logging.warning ("idletime module returned an invalid value...")
  66.             seconds = 0.0
  67.  
  68.         if self.idling:
  69.             self._whenIdling(seconds)
  70.         else:
  71.             self._whenNotIdling(seconds)
  72.         self.lastTimeout = eventloop.addTimeout(self.periodicity,self.run,"Idle notifier")
  73.  
  74.  
  75.     def join(self):
  76.         try:
  77.             self.lastTimeout.cancel()
  78.         except:
  79.             pass
  80.     
  81.     def _whenNotIdling(self, seconds):
  82.         if seconds >= self.idleThreshold:
  83.             logging.info ("system has been idling since %d seconds", seconds)
  84.             self.idling = True
  85.             self.wasIdling = True
  86.             if self.delegate is not None and hasattr(self.delegate, 'systemHasBeenIdlingSince'):
  87.                 self.delegate.systemHasBeenIdlingSince(seconds)
  88.  
  89.     def _whenIdling(self, seconds):
  90.         if seconds < self.idleThreshold:
  91.             self.idling = False
  92.             if self.wasIdling:
  93.                 logging.info ("system is active again since %d seconds", seconds)
  94.                 if self.delegate is not None and hasattr(self.delegate, 'systemIsActiveAgain'):
  95.                     self.delegate.systemIsActiveAgain()
  96.                     self.wasIdling = False
  97.